package com.cmw.auntapp.component;

import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.widget.Button;

import com.cmw.auntapp.R;

/**
 * @desc: 倒计时插件
 * @author: chengmingwei
 * @date: 2021/4/27 6:57 AM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class SendCountDownTimer extends CountDownTimer {
    private Button countDownBtn;

    private Drawable nomalBackground;

    private Drawable activeBackground;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public SendCountDownTimer(Button countDownBtn, Drawable nomalBackground, Drawable activeBackground,
                              long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.countDownBtn = countDownBtn;
        this.nomalBackground = nomalBackground;
        this.activeBackground = activeBackground;
    }


    /**
     * 计时过程
     *
     * @param millisUntilFinished
     */
    @Override
    public void onTick(long millisUntilFinished) {
        //防止计时过程中重复点击
        countDownBtn.setClickable(false);
        countDownBtn.setBackground(nomalBackground);
        countDownBtn.setText(millisUntilFinished/1000+"s");
    }

    /**
     * 计时完毕的方法
     *
     */
    @Override
    public void onFinish() {
        //重新给Button设置文字
        countDownBtn.setText("重新获取");
        countDownBtn.setBackground(activeBackground);
        //设置可点击
        countDownBtn.setClickable(true);

    }
}
